#include "ManualCalibViewModel.h"
#include <QColor>

ManualCalibViewModel::ManualCalibViewModel()
{
}

ManualCalibViewModel::~ManualCalibViewModel()
{
}

QVariant ManualCalibViewModel::data(const QModelIndex &idx, int role) const
{
    QVariant value = QSqlTableModel::data(idx, role);

    if(Qt::TextAlignmentRole == role)
    {
        value = int(Qt::AlignCenter | Qt::AlignHCenter);
        return value;
    }
    else if(Qt::ForegroundRole == role)
    {
        if(idx.column() == 2)
        {
            //输入范围不在75.0-125.0之间的数据标红
            QString str = idx.model()->data(idx, Qt::EditRole).toString();
            if(str.trimmed().isEmpty() == false)
            {
                double t  = idx.model()->data(idx, Qt::EditRole).toDouble();
                if(t < 75.0 || t > 125.0)
                {
                  return QColor(Qt::red);
                }
            }
        }
    }
    else if(Qt::BackgroundColorRole == role)
    {
        if(idx.column()== 1)
        {
            return QColor(96, 204, 152);
        }
    }
    else if(Qt::FontRole == role)
    {

    }

    return value;
}

Qt::ItemFlags ManualCalibViewModel::flags(const QModelIndex &index) const
{

    Qt::ItemFlags flags = QAbstractItemModel::flags(index);

    //设置第2列可编辑，其他列为不可编辑项
    if (index.column() == 2) {
            flags |= Qt::ItemIsEditable;
    }
    return flags;
}
